package com.dunqi.gpm.shunqing.home.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.dunqi.gpm.shunqing.R;
import com.dunqi.gpm.shunqing.common.api.ApiFactory;
import com.dunqi.gpm.shunqing.common.api.RxSchedulers;
import com.dunqi.gpm.shunqing.common.base.BaseAct;
import com.dunqi.gpm.shunqing.common.base.ComObserver;
import com.dunqi.gpm.shunqing.common.base.CommonAdapter;
import com.dunqi.gpm.shunqing.common.base.CommonViewHolder;
import com.dunqi.gpm.shunqing.common.base.Data;
import com.dunqi.gpm.shunqing.common.base.RecyclerAdapter;
import com.dunqi.gpm.shunqing.common.util.SwipeUtil;
import com.dunqi.gpm.shunqing.home.bean.IndexList;
import com.dunqi.gpm.shunqing.home.bean.MeetingRoom;
import com.dunqi.gpm.shunqing.home.bean.ProList;
import com.dunqi.gpm.shunqing.home.widget.EmptyView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;

public class MeetingRoomActivity extends BaseAct implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {


    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.swipeRefresh)
    SwipeRefreshLayout swipeRefresh;
    List<MeetingRoom> data = new ArrayList<>();
    private MeetingRoomAdapter adapter;

    @Override
    protected void refreshData(Bundle savedInstanceState) {
        net();
    }

    private void net() {
        ApiFactory.getInstance()
                .liveMeetingGet(null)
                .compose(RxSchedulers.<Data<ArrayList<MeetingRoom>>>io_main())
                .subscribe(new ComObserver<ArrayList<MeetingRoom>>() {
                    @Override
                    public void onSuccess(ArrayList<MeetingRoom> pages) {
//                        ArrayList<MeetingRoom> list = pages.getList();
                        adapter.replaceData(pages);
//                        if (pages.isHasNextPage()) {
//                            adapter.loadMoreComplete();
//                        } else {
//                            adapter.loadMoreEnd();
//                        }
//
//                        if (page == 1) {
//
//                        } else {
//                            adapter.addData(list);
//                        }
                        adapter.loadMoreEnd(true);
                        page++;
                    }

                    @Override
                    public void onFinal() {
                        SwipeUtil.loadCompleted(swipeRefresh);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        adapter.loadMoreFail();
                    }

                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposables(d);
                    }
                });
    }

    @Override
    protected void initUiAndListener() {

        SwipeUtil.init(swipeRefresh);
        recycler.setLayoutManager(new LinearLayoutManager(this));
        adapter = new MeetingRoomAdapter(data);
        recycler.setAdapter(adapter);
        adapter.setEmptyView(new EmptyView(this));

        swipeRefresh.setOnRefreshListener(this);
        adapter.setOnLoadMoreListener(this, recycler);

    }

    @Override
    public void onRefresh() {
        page = 1;
        net();
    }

    @Override
    public void onLoadMoreRequested() {
        net();
    }

    class MeetingRoomAdapter extends RecyclerAdapter<MeetingRoom> {

        public MeetingRoomAdapter(@Nullable List<MeetingRoom> data) {
            super(R.layout.item_meeting_room, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, MeetingRoom item) {
            GridView gridView = helper.getView(R.id.grid_view);
            TextView nameMeetingRoomTv = helper.getView(R.id.name_meeting_room_tv);
            nameMeetingRoomTv.setText(item.getName());

            final List<ProList> proList = item.getProList();
            gridView.setAdapter(new CommonAdapter<ProList>(helper.itemView.getContext(), proList) {
                @Override
                public int getLayout(int position) {
                    return R.layout.item_video_name;
                }

                @Override
                public void bindData(CommonViewHolder cvh, int position) {
                    cvh.setText(R.id.video_name, getItem(position).getName());
                }
            });
            gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    ProList pro = proList.get(position);
                    ArrayList<IndexList> list = pro.getIndexList();
                    VideoActivity.start(getActivity(), list);
                }
            });
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_meeting_room;
    }
}
